package com.cskaoyan._14network._04test._02feedback;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Macro_Ray
 * @since 2024/3/26 10:19
 */
// 服务区：接受数据并打印，再给客户端反馈消息
public class Server {
    public static void main(String[] args) throws IOException {
        // 1.创建ServerSocket对象，绑定端口
        ServerSocket ss = new ServerSocket(10086);
        // 2.监听客户端连接
        Socket socket = ss.accept();
        // 3.接收客户端数据并打印
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        int b =0;
        // read方法会从连接通道中读取数据，但是需要一个结束标记才会停止循环
        while((b = br.read())!= -1) {
            System.out.print((char) b);
        }
        // 4.给客户端反馈消息
        String str="魂殿小儿！";
        OutputStream os = socket.getOutputStream();
        os.write(str.getBytes());

        // 5.释放资源
        ss.close();
        socket.close();

    }
}
